﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace Cog
{
    class Program
    {
        private static readonly List<DataTable> Tables = new List<DataTable>();
        private static readonly string[] ValidArgs = new[] {"namespace=","schema=","connection=","dest=", "relationship=", "classprefix="}; 
 
        static int Main(string[] args)
        {
            var arguments = ValidArgs.ToDictionary(validArg => validArg,
                                                    validArg => args.FirstOrDefault(a => a.ToLower().StartsWith(validArg)));

            arguments = arguments.ToDictionary(v => v.Key, v => (v.Value ?? "").Replace(v.Key, string.Empty));

            if (string.IsNullOrEmpty(arguments["relationship="]))
                arguments["relationship="] = "true";

            if (string.IsNullOrEmpty(arguments["classprefix="]))
                arguments["classprefix="] = arguments["schema="];

            if (arguments.Any(a => string.IsNullOrEmpty(a.Value)))
            {
                Console.WriteLine("Generates a data access layer C# project.\r\n");
                Console.WriteLine("COG namespace=<namespace> schema=<database schema> connection=<connection string> dest=<destination folder> relation=[true/false] classprefix=[class prefix, defaults to database schema]\r\n");
                foreach (var argument in arguments.Where(argument => argument.Value == null))
                {
                    Console.WriteLine("Argument \"{0}\" not specified.", argument.Key);
                }
                return 1;
            }

            var cog = new CodeGen(arguments["namespace="], arguments["schema="], arguments["connection="],
                                  arguments["dest="], arguments["relationship="].Equals("true"), arguments["classprefix="]);
            cog.Build();

            return 0;
        }
    }
}
